package com.itheima.service.mongo.impl;

import com.itheima.domain.db.RecommendUser;
import com.itheima.service.mongo.RecommendUserService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * @author zsk
 * @create 2022-03-20-15:40
 */
@DubboService
public class RecommendUserServiceImpl implements RecommendUserService {

    @Autowired
    private MongoTemplate mongoTemplate;

    //根据登陆用户id查询今日佳人
    @Override
    public RecommendUser findTodayBest(Long toUserId) {
        //构建查询条件
        Query query = new Query(
                Criteria.where("toUserId").is(toUserId))//条件为登录userId
                .with(Sort.by(Sort.Order.desc("score")))//按照推荐得分降序
                .skip(0).limit(1);//只查询第一条

        //查询
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);

        //返回结果
        return recommendUser;
    }

    @Override
    public PageBeanVo findRecommendUserVoByPage(Long toUserId, Integer pageNum, Integer pageSize) {

        Integer index = (pageNum - 1) * pageSize +1;

        //构建查询条件
        Query query = new Query(
                Criteria.where("toUserId").is(toUserId))
                .with(Sort.by(Sort.Order.desc("score")))//按缘分值倒序
                .skip(index).limit(pageSize);

        //查询
        List<RecommendUser> recommendUserList = mongoTemplate.find(query, RecommendUser.class);

        //返回pageBean对象
        return new PageBeanVo(pageNum,pageSize,0L,recommendUserList);
    }

    //根据id查询缘分值对象
    @Override
    public RecommendUser findPersonalInfo(Long toUserId, Long jiarenId) {

        //构建查询条件
        Query query = new Query(
                Criteria.where("toUserId").is(toUserId)
                .and("userId").is(jiarenId)
        );

        //查询并返回
        return mongoTemplate.findOne(query, RecommendUser.class);

    }
}
